package com.xiaotu.spider.project.pageprocessor.list;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;

/**
 * @类名 QQCommentListPageProcessor
 * @日期 2017年8月25日
 * @作者 王艳龙
 * @功能 腾讯短评爬取类
 */
@Component("QQCommentListPageProcessor")
public class QQCommentListPageProcessor extends AUrlPageProcessor{
	
	 	private String fileName; //读取tvName文件路径
	    private String QQComment; //QQ短评
	
	@Override
    public void initRunParams(Map<String, Object> map)
    {
        if (map.get("fileName") == null)
            throw new SpiderException(
                    SpiderExceptionCode.PageProcessorParameterErrorException);
        fileName = map.get("fileName") + StringUtils.EMPTY;

        if (map.get("QQComment") == null)
            throw new SpiderException(
                    SpiderExceptionCode.PageProcessorParameterErrorException);
        QQComment = map.get("QQComment") + StringUtils.EMPTY;
    }	
		
    @Override
    protected String[] getStartRequestUrls(){
    	 List<JSONObject> targetList = readFileColumn(fileName); //读取excel电影名
         List<String> linkList = new ArrayList<String>();
         for (JSONObject target : targetList){
             linkList.add("http://s.video.qq.com/search?comment=1&stype=0"
				+ "&plat=2&otype=json&query="
				+ KeywordEncode.encode(target.getString("tvName"))
				+ "&pver=0&tabid=0&sort=0&cur=0"
				+ "&num=0&start=0&end=20&stag=txt.historyword");
         }
         String[] urlArr = new String[linkList.size()];
         linkList.toArray(urlArr);
         return urlArr;
    }

    @Override
    public String getPageDataType(Page page)
    {
    	String url = page.getUrl().toString();
		if (url.contains("http://s.video.qq.com/search"))
			return Constants.PageDataType.TV_RECORD;
		else if (url.contains("http://coral.qq.com/article/"))
			return Constants.PageDataType.TV_COMMENT;
		return Constants.PageDataType.TV_PRO_COMMENT;
    }
    
    @Override
    public void process(Page page){
    	Map<String, Object> tvMap = page.getRequest().getExtras();
    	String url = page.getUrl().toString();
    	if(StringUtils.isNotEmpty(url) && url.contains("query") && url.contains("pver")){
    		String tvName = url.substring(url.indexOf("&query=")+7, url.indexOf("&pver"));
    		tvMap.put("tvName", tvName);
    	}
	    switch (this.getPageDataType(page)){
	    case Constants.PageDataType.TV_RECORD:
			this.tvRecordProcess(page, tvMap);
			break;
		case Constants.PageDataType.TV_COMMENT:
			this.commentProcess(page, tvMap);
			break;
		default:
			break;
	    }
}

    private void tvRecordProcess(Page page, Map<String, Object> tvMap)
	{
		String searchList = page.getRawText();
		if (StringUtils.isEmpty(searchList))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		searchList = searchList.replace("QZOutputJson=", StringUtils.EMPTY);
		searchList = searchList.substring(0, searchList.lastIndexOf(";"));
		JSONObject searchJson = JSONObject.fromObject(searchList);
		if (searchJson == null || searchJson.isEmpty()
				|| !searchJson.has("list"))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		JSONArray array = searchJson.getJSONArray("list");
		if (null == array || array.size() == 0)
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
			JSONObject item = array.getJSONObject(0);
			String commentId = HttpUtils.httpGetJSONP(
					"http://sns.video.qq.com/fcgi-bin/video_comment_id?otype=json&"
							+ "callback=jQuery191026196013600565493_1442389818168&low_login=1&op=3&cid="
							+ item.getString("ID") + "&_=1442389818169");
			if (StringUtils.isNotEmpty(commentId) && commentId.contains("{")
					&& commentId.contains("}"))
			{
				commentId = commentId.substring(commentId.indexOf("{"),
						commentId.lastIndexOf("}") + 1);
				JSONObject commentObject = JSONObject.fromObject(commentId);
					
					String linkUrl ="http://coral.qq.com/article/"
							+ commentObject.get("comment_id")
							+ "/comment?commentid=0&reqnum=10&tag=&callback=jQuery111106699177431873977_1442473902977&_=1442473902978";
					PageUtils.addTargetRequest(page,linkUrl,page.getRequest().getExtras());// 添加评论链接
			}
	}
	
	private void commentProcess(Page page, Map<String, Object> tvMap)
	{
		
		String commentListJSONP = page.getRawText();
		if (StringUtils.isEmpty(commentListJSONP))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		commentListJSONP = commentListJSONP.substring(
				commentListJSONP.indexOf("{"),
				commentListJSONP.lastIndexOf("}") + 1);
		
		JSONObject commentListObject = JSONObject.fromObject(commentListJSONP);
		
		if (commentListObject == null || commentListObject.isEmpty()
				|| !commentListObject.has("data"))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		JSONArray commentList = commentListObject.getJSONObject("data")
				.getJSONArray("commentid");
		String comment_id =commentListObject.getJSONObject("data").getString("targetid");
		String lastId = commentListObject.getJSONObject("data").getString("last");
		JSONObject comment = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		if (null != commentList && commentList.size() > 0)
		{
			for (int i = 0; i < commentList.size(); i++){
				comment = commentList.getJSONObject(i);
				Date data = new Date(Long.valueOf(comment.getString("time")) * 1000);
				String comTime =sdf.format(data);
				String line = StringUtils.join(new String[]{StringUtil.nullToString(page.getRequest().getExtra("type")),
						KeywordEncode.decode((String) tvMap.get("tvName")), comment.getJSONObject("userinfo").getString("nick"), 
						comTime, comment.getString("content")}, SepratorUtil
		                .SEP_PIPE);
		        System.out.println(line);
		        FileUtil.writeFile(QQComment, line + SepratorUtil.LINE_SEPRATOR);
			}
			
		}
			if(!lastId.equals("false")){
				String linkUrl ="http://coral.qq.com/article/"
						+ comment_id
						+ "/comment?commentid="+lastId+"&reqnum=20&tag=&callback=jQuery111106699177431873977_1442473902977&_=1442473902978";
				PageUtils.addTargetRequest(page,linkUrl,page.getRequest().getExtras());// 添加评论链接
			}else{
				return;
			}
	}
	
    /**
     * 读取文件指定列
     *
     * @param fileName 文件名称
     * @return 行列表
     */
    public static List<JSONObject> readFileColumn(String fileName)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        Workbook wb = null;
        String json;
        List<JSONObject> list = new ArrayList<JSONObject>();
        try
        {
            InputStream inputStream = new FileInputStream(fileName);
            if (fileName.endsWith("xls"))
            {
                wb = new HSSFWorkbook(inputStream); // 解析xls格式
            } else if (fileName.endsWith("xlsx"))
            {
                wb = new XSSFWorkbook(inputStream); // 解析xlsx格式
            }
            Sheet sheet = wb.getSheetAt(0); // 第一个工作表

            int firstRowNum = 1;
            int lastRowNum = sheet.getLastRowNum();

            Row row = null;
            Cell cell_a1 = null;
            for (int i = firstRowNum; i <= lastRowNum; i++)
            {
                row = sheet.getRow(i);          //取得第i行
                cell_a1 = row.getCell(0);      //取得i行的第一列
                if(row.getCell(0)!=null){
                	cell_a1.setCellType(Cell.CELL_TYPE_STRING);
                    String cellValue1 = cell_a1.getStringCellValue();
                    map.put("tvName", cellValue1);
               }
                json = new Gson().toJson(map);
                JSONObject jsStr = JSONObject.fromObject(json);
                list.add(jsStr);
            }

        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return list;
    }
    
    @Override
    protected String getSpiderType()
    {
    	return "QQCommentList";
    }
}
